package com.ccb.sc.order.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccb.sc.order.mapper.AppOrderExchangeMapper;
import com.ccb.sc.order.vo.OrderExchangeCreateVo;
import com.ccb.sc.order.vo.OrderExchangeListVo;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.modules.live.enums.LiveStreamingOrderExchangeStatus;
import com.ccb.sc.common.modules.live.enums.LiveStreamingOrderSource;
import com.ccb.sc.common.modules.order.entity.OrderExchange;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @author ken
 * @version 1.0
 * @date 2022/8/14 15:00
 */
@Service
public class AppOrderExchangeService extends ServiceImpl<AppOrderExchangeMapper, OrderExchange> {

    public PageVo<OrderExchangeListVo> selectOrderExchanges(Long memberId, PageParams queryParam) {
        Page<OrderExchange> page = new Page<OrderExchange>(queryParam.getCurrent(), queryParam.getPageSize());
        AppOrderExchangeMapper.QueryParam params = new AppOrderExchangeMapper.QueryParam();
        params.setMemberId(memberId);
        Page<OrderExchangeListVo> orderExchangePage = this.getBaseMapper().selectByQueryParam(page, params);
        return PageVo.create(queryParam.getCurrent(), queryParam.getPageSize(), orderExchangePage.getTotal(), orderExchangePage.getRecords());
    }

    public void create(Long memberId, OrderExchangeCreateVo createVo) {
        OrderExchange orderExchange = new OrderExchange();
        orderExchange.setMemberId(memberId);
        orderExchange.setOrderId(createVo.getOrderId());
        LiveStreamingOrderSource orderSource = LiveStreamingOrderSource.getByValue(createVo.getSource());
        Assert.notNull(orderSource, "订单来源错误！");
        long existCount = this.count(Wrappers.<OrderExchange>lambdaQuery()
                .eq(OrderExchange::getOrderId, createVo.getOrderId())
                .eq(OrderExchange::getSource, orderSource));
        Assert.isTrue(existCount == 0, "订单已存在，请勿重复申请！");
        orderExchange.setSource(orderSource);
        orderExchange.setFansLevel(createVo.getFansLevel());
        orderExchange.setExchangeStatus(LiveStreamingOrderExchangeStatus.UN_FINISHED);
        this.save(orderExchange);
    }



}
